package com.siemens.industrial.web.mvc.controller;

import com.siemens.industrial.utils.*;
import com.siemens.industrial.web.mvc.service.ExcelService;
import com.siemens.industrial.web.mvc.service.ImageService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/13.
 */
@Controller
public class ImageController {
    @Resource
    ImageService imageService;

    @RequestMapping(value = "/api/v1/image/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> uploadImage(HttpServletRequest request, HttpServletResponse response) {
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            String imagePath = ConfigurationUtil.INSTANCE.getImagePath();
            File file = new File(imagePath);
            factory.setRepository(file);//设置缓存路径
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items = upload.parseRequest(request);
            if (items.size() != 1) {
                return RestResponse.create(GlobalResponseCode.ERROR_EVENT_NOT_EXISTED).build();
            }

            String name = imageService.uploadImage("", items);
            return RestResponse.create(GlobalResponseCode.SUCCESS).put("name", name).put("url", "/api/v1/image/get?name=" + name).build();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/api/v1/image/get", method = RequestMethod.GET, produces = "image/x-png")
    @ResponseBody
    public byte[] getImageByName(@RequestParam(name = "name") String name) {
        try {
            return ImageService.getImage(name);
        } catch (SiemensException e) {
            return null;
        }
    }

    @RequestMapping(value = "/api/v1/excel/get", method = RequestMethod.GET)
    public void getEByName(@RequestParam(name = "name") String name, HttpServletResponse response) {
        try {
            byte[] bytes = ExcelService.getExcelBytes(name);
            if (bytes == null)
                return;
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xlsx");
            response.setContentLength(bytes.length);
            try {
                response.getOutputStream().write(bytes);
                response.getOutputStream().flush();
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (SiemensException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/api/v1/deviceXml/get", method = RequestMethod.GET)
    public void getXmlByName(@RequestParam(name = "name") String name, HttpServletResponse response) {
        try {
            byte[] bytes = ExcelService.getDeviceXmlBytes(name);
            if (bytes == null)
                return;
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xml");
            response.setContentLength(bytes.length);
            try {
                response.getOutputStream().write(bytes);
                response.getOutputStream().flush();
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (SiemensException e) {
            e.printStackTrace();
        }
    }
}
